var e,
  t = require("../../@babel/runtime/helpers/interopRequireDefault"),
  r = t(require("../../@babel/runtime/regenerator")),
  n = t(require("../../@babel/runtime/helpers/asyncToGenerator")),
  a = require("../utils/util"),
  i = require("../comm/packet"),
  o = require("../comm/rmodel").detectM,
  u = require("../comm/rcode"),
  c = require("../../buildConfig").BuildConfig,
  s = require("../comm/local"),
  l = require("./touchHandle"),
  d = {
    timestamp: 0,
    sdkInfo: "",
    deviceObj: {
      1: "",
      2: "",
      3: "",
      4: "",
      5: "",
      7: "",
      8: "",
      9: "",
      10: "",
      13: "",
      14: "",
      20: "",
      21: "",
      24: "",
      25: "",
      31: "",
      32: "",
      34: "",
      35: "",
      40: "",
      41: "",
      42: "",
      43: "",
      121: "",
      122: "",
      123: "",
      124: "",
    },
    productInfo: { clientVer: "", requestPackageName: "" },
  };
function f(e, t, r) {
  (d.timestamp = new Date().getTime()),
    (d.deviceObj[1] = t),
    (d.deviceObj[2] = e);
  var n = wx.getSystemInfoSync();
  (d.deviceObj[4] = n.platform + ""),
    (d.deviceObj[124] = n.enableDebug),
    null != r.productInfo &&
      null != r.productInfo &&
      (d.productInfo = r.productInfo),
    (d.sdkInfo = {
      buildno: c.buildNo,
      sdkver: c.sdkVersion,
      lc: c.lc,
      channel: r.channel,
      platform: 5,
    });
}
function p(e) {
  return new Promise(function (t, r) {
    e.getLngAndLat(t),
      setTimeout(function () {
        t("");
      }, 3e3);
  });
}
function m(e) {
  return new Promise(function (t, r) {
    e.getWifiBssid(t),
      setTimeout(function () {
        t("");
      }, 3e3);
  });
}
function T() {
  var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 0,
    r = arguments.length > 1 ? arguments[1] : void 0;
  if (!a.isEmpty(r)) {
    var n = new Date().getTime(),
      i = n + 1e3 * t,
      o = r + "||" + n + "||" + i;
    s.setLocal(s.dTokenKey(e.salt), o);
  }
}
function v(e, t, r, n, a) {
  return g.apply(this, arguments);
}
function g() {
  return (g = (0, n.default)(
    r.default.mark(function t(n, c, v, g, k) {
      var b, h, y;
      return r.default.wrap(function (t) {
        for (;;)
          switch ((t.prev = t.next)) {
            case 0:
              if (
                ((b = s.getUUID(
                  s.uidKey(e.salt),
                  s.tTokenKey(e.salt),
                  s.rTokenKey(e.salt),
                )),
                f(n, b, c),
                void 0 === c.implement || null === c.implement)
              ) {
                t.next = 13;
                break;
              }
              if (void 0 === (h = c.implement).getLngAndLat) {
                t.next = 8;
                break;
              }
              return (t.next = 7), p(h);
            case 7:
              d.deviceObj[121] = t.sent;
            case 8:
              if (
                (void 0 !== h.getTouchs &&
                  (d.deviceObj[122] = l.getAllTouchs()),
                void 0 === h.getWifiBssid)
              ) {
                t.next = 13;
                break;
              }
              return (t.next = 12), m(h);
            case 12:
              d.deviceObj[123] = t.sent;
            case 13:
              (y = i.encryptReqPacket(
                d,
                d.sdkInfo.channel,
                d.deviceObj[1],
                s.rTokenKey(e.salt),
              )),
                v.request(
                  y,
                  function (t) {
                    var r = t.resp;
                    (o.ret = u.REQUEST_INIT),
                      r &&
                        r.data &&
                        r.data.resp &&
                        ((o.ret = r.data.resp.ret),
                        (o.deviceToken = r.data.resp.msgBlock),
                        k && T(r.data.resp.overtime, o.deviceToken)),
                      null != g && g(o),
                      r.data &&
                        r.data.resp &&
                        !a.isEmpty(r.data.resp.token) &&
                        s.setLocal(s.rTokenKey(e.salt), r.data.resp.token);
                  },
                  function (e) {
                    (e = "number" != typeof e ? 0 : e),
                      (o.ret = -22e3 - e),
                      (o.deviceToken = ""),
                      null != g && g(o);
                  },
                  c.riskUrl,
                );
            case 15:
            case "end":
              return t.stop();
          }
      }, t);
    }),
  )).apply(this, arguments);
}
function k() {
  return (k = (0, n.default)(
    r.default.mark(function t(n, i, c, l, d) {
      var f, p, m, T, g, k, b, h;
      return r.default.wrap(function (t) {
        for (;;)
          switch ((t.prev = t.next)) {
            case 0:
              if (!a.isEmpty(n)) {
                t.next = 5;
                break;
              }
              return (
                (o.ret = u.ERROR_BIZ_DETECT_NOTICKET),
                (o.deviceToken = ""),
                l(o),
                t.abrupt("return")
              );
            case 5:
              if (!d) {
                t.next = 22;
                break;
              }
              if (((f = s.getLocal(s.dTokenKey(e.salt))), !a.isEmpty(f))) {
                t.next = 10;
                break;
              }
              return v(n, i, c, l, d), t.abrupt("return");
            case 10:
              if (
                ((p = f.split("||")),
                (m = p[0]),
                (T = p[1]),
                (g = p[2]),
                (k = new Date().getTime()),
                (b = k - T),
                (h = g - T),
                !(Math.abs(b) < Math.abs(h)))
              ) {
                t.next = 20;
                break;
              }
              return l({ ret: 0, deviceToken: m }), t.abrupt("return");
            case 20:
              t.next = 23;
              break;
            case 22:
              s.delLocal(s.dTokenKey(e.salt));
            case 23:
              v(n, i, c, l, d);
            case 24:
            case "end":
              return t.stop();
          }
      }, t);
    }),
  )).apply(this, arguments);
}
module.exports = {
  initRisk: function (t) {
    e = t;
    var r = t.implement;
    null != r &&
      void 0 !== r.getTouchs &&
      r.getTouchs({
        onTouch: function (e, t) {
          if (null !== t && null != t) {
            var r = {
              viewId: "",
              touchTimestamp: 0,
              touchX: 0,
              touchY: 0,
              targetId: 0,
              targetOffsetLeft: 0,
              targetOffsetTop: 0,
              type: "",
              touches: null,
            };
            (r.viewId = e),
              (r.touchTimestamp = t.timeStamp),
              (r.touchX = t.detail.x ? t.detail.x.toFixed(2) : 0),
              (r.touchY = t.detail.y ? t.detail.y.toFixed(2) : 0),
              (r.targetId = t.currentTarget.id),
              (r.targetOffsetLeft = t.currentTarget.offsetLeft
                ? t.currentTarget.offsetLeft.toFixed(2)
                : 0),
              (r.targetOffsetTop = t.currentTarget.offsetTop
                ? t.currentTarget.offsetTop.toFixed(2)
                : 0),
              (r.type = t.type),
              (r.touches = t.touches),
              l.cache(r);
          }
        },
      });
  },
  getDeviceToken: function (e, t, r, n, a) {
    return k.apply(this, arguments);
  },
};
